package org.gaaidou.ptarmigan.springboot.core.async;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import java.util.concurrent.CompletionStage;

@Component
public class AsyncCaller {

    @Autowired
    @Qualifier("asyncExecutor")
    private ThreadPoolTaskExecutor executor;

    public <T> CompletionStage<T> create(T value) {
        CompletableFuture<T> completableFuture = new CompletableFuture<>(executor);
        completableFuture.complete(value);
        return completableFuture;
    }

    public CompletionStage<Void> create() {
        CompletableFuture<Void> completableFuture = new CompletableFuture<>(executor);
        completableFuture.complete(null);
        return completableFuture;
    }

    @Async
    public void exec(Runnable r) {
        r.run();
    }
}
